package proyecto.gestionarDatosDeTemas;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import proyecto.BusinessLogic.TemaBLRemote;
import proyecto.EntityBeans.Tema;
import util.Common;

public class GestionarDatosDeTemasMultiAction extends DispatchAction {

	/*
	 * lista de temas
	 */
	public ActionForward listaTemas(
			ActionMapping mapping,
			ActionForm form,			
			HttpServletRequest request,
			HttpServletResponse response) {
		
		TemaBLRemote temaBLRemote = null;
		final Context context;
		
		ListaTemasForm listaTemasForm = (ListaTemasForm) form;
		
		try
		{							
			context = new InitialContext();					
			temaBLRemote = (TemaBLRemote) context.lookup("TemaBL/remote");
			
			listaTemasForm.setTemas(temaBLRemote.findAll());
			
			return mapping.findForward(Common.SHOWLIST);
			
		} catch (NamingException e)				{
			e.printStackTrace();
			throw new RuntimeException(e);				
		}	
	}
	
	/*
	 * alta de temas
	 */
	public ActionForward altaTema(
			ActionMapping mapping,
			ActionForm form,			
			HttpServletRequest request,
			HttpServletResponse response) {
						
			TemaBLRemote temaBLRemote = null;
			final Context context;
		
			TemaForm temaForm = (TemaForm) form;			
											
			if (isCancelled(request)){				
				return mapping.findForward(Common.SHOWLIST);
			}
			else{			
				try
				{							
					context = new InitialContext();					
					temaBLRemote = (TemaBLRemote) context.lookup("TemaBL/remote");
					
					//creo el tema				
					Tema tema = new Tema(temaForm.getNombre(),temaForm.getDescripcion(),new Date());
					
					//alta de tema
					int temaNuevo = temaBLRemote.save(tema); //alta de tema
					temaForm.setIdTema(temaNuevo);
					
					return mapping.findForward(Common.SUCCESS);	
					
				} catch (NamingException e)				{
					e.printStackTrace();
					throw new RuntimeException(e);				
				}	
							
		}
		
	}
	
	/*
	 * eliminacion de tema
	 */
	public ActionForward eliminarTema(
			ActionMapping mapping,
			ActionForm form,			
			HttpServletRequest request,
			HttpServletResponse response) {
						
			TemaBLRemote temaBLRemote = null;
			final Context context;
		
			TemaForm temaForm = (TemaForm) form;			
											
			if (isCancelled(request)){				
				return mapping.findForward(Common.SHOWLIST);
			}
			else{			
				try
				{							
					context = new InitialContext();					
					temaBLRemote = (TemaBLRemote) context.lookup("TemaBL/remote");
					
					//Eliminar curso	
					if (!temaBLRemote.findById(temaForm.getIdTema()).equals(null)){
						temaBLRemote.remove(temaForm.getIdTema());	
					}	
					
					return mapping.findForward(Common.SHOWLIST);
					
				} catch (NamingException e)				{
					e.printStackTrace();
					throw new RuntimeException(e);				
				}	
		}
	}
	
	/*
	 * Modificar tema
	 */
	public ActionForward modificarTema(
			ActionMapping mapping,
			ActionForm form,			
			HttpServletRequest request,
			HttpServletResponse response) {
						
		    TemaBLRemote temaBLRemote = null;
			final Context context;
			Date fechaAlta = null;
		
			TemaForm temaForm = (TemaForm) form;
			
			SimpleDateFormat targetDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
											
			if (isCancelled(request)){				
				return mapping.findForward(Common.SHOWLIST);
			}
			else{			
				try
				{							
					context = new InitialContext();					
					temaBLRemote = (TemaBLRemote) context.lookup("TemaBL/remote");
					
					String fecha = temaForm.getFechaAlta();
					fechaAlta = targetDateFormat.parse(fecha);	
					
					//update de tema				
					Tema tema = new Tema(temaForm.getIdTema(),temaForm.getNombre(),temaForm.getDescripcion(),fechaAlta);
					temaBLRemote.merge(tema);
					
				} catch (NamingException e)				{
					e.printStackTrace();
					throw new RuntimeException(e);				
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
							
				return mapping.findForward(Common.SHOWLIST);				
		}
	}
	
	/*
	 * Obtener datos de tema
	 */
	protected ActionForm construirTemaForm(String id)
				throws Exception {
			
		TemaForm form = null;
		TemaBLRemote temaBLRemote = null;
		Tema tema = null;
		final Context context;
				
		try {	
			context = new InitialContext();					
			temaBLRemote = (TemaBLRemote) context.lookup("TemaBL/remote");
		}
		catch (NamingException e)				{
			e.printStackTrace();
			throw new RuntimeException(e);				
		}
			
		Integer idTema = Integer.valueOf(id);		
		tema = temaBLRemote.findById(idTema);
			
		if ( tema != null ) {
			form = new TemaForm();
			form.setIdTema(tema.getIdTema());
			form.setNombre(tema.getNombre());
			form.setDescripcion(tema.getDescripcion());
			form.setFechaAlta(String.valueOf(tema.getFechaAlta()));
		}
		else {
			throw new Exception("Tema " + idTema + " no encontrado!");
		}
			
		return form;
	}
			
			
	public ActionForward obtenerTema(
				ActionMapping mapping,
				ActionForm form,			
				HttpServletRequest request,
				HttpServletResponse response) {
									
			try {
				
				form = this.construirTemaForm(request.getParameter("idTema"));
									
				if ("request".equals(mapping.getScope())) {				
					request.setAttribute(mapping.getAttribute(), form);
				}				
					
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
												
			return mapping.findForward(Common.SUCCESS);				
	}
}
